package moc.zard.xg.controller;


import com.google.common.collect.Lists;
import moc.zard.xg.moc_utils.StatusUtils;
import moc.zard.xg.pojo.Person;
import moc.zard.xg.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/person")
public class PersonController {


    @Autowired
    private moc.zard.xg.service.IPersonrService IPersonrService;


    @RequestMapping("save")
    public Result person_save(Person person) {
        Result result = Result.setOK();
        try {
            Person save = IPersonrService.save(person);
            result.setData(Lists.newArrayList(save));
        } catch (Exception e) {
            StatusUtils.setSorry(result);
        }

        return result;
    }


    @GetMapping("findByName")
    public Result person_findByName(String name) {
        Result result = Result.setOK();
        try {
            Person byName = IPersonrService.findByName(name);
            result.setData((List<Object>) byName);
        } catch (Exception e) {
            StatusUtils.setSorry(result);
        }
        return result;
    }

    @PostMapping("/findById")
    public Result person_findById(@RequestParam(name = "id") Integer id) {
        Result result = Result.setOK();
        try {
            Person save = IPersonrService.findById(id);
            result.setData(Lists.newArrayList(save));
        } catch (Exception e) {
            StatusUtils.setSorry(result);
        }

        return result;
    }

}
